package com.learning.demos;

import com.learning.enums.CountryEnum;

import java.util.concurrent.CountDownLatch;

/**
 * @author adolphw
 */
public class CountDownLatchDemo {

    public static void main(String[] args) throws Exception {
        CountDownLatch countDownLatch = new CountDownLatch(6);

        for (int i = 1; i <=6; i++) {

            new Thread(() -> {

                System.out.println(Thread.currentThread().getName() + "\t  国 ，被灭");
                countDownLatch.countDown();
            }, CountryEnum.forEach_CountryEnum(i).getRetMessage()).start();

            System.out.println(CountryEnum.ONE);
            System.out.println(CountryEnum.ONE.getRetCode());
            System.out.println(CountryEnum.ONE.getRetMessage());
        }

        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + "\t leader 秦  一统华夏");

    }


    public static void  leave() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);

        for (int i = 0; i < 6; i++) {

            new Thread(() -> {

                System.out.println(Thread.currentThread().getName() + "\t worker done and leave");
                countDownLatch.countDown();
            }, String.valueOf(i)).start();
        }

        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + "\t leader done and leave");
    }
}
